package com.example.project10;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

/**
 * 书籍列表适配器
 * 用于在RecyclerView中显示书籍信息
 */
public class BookAdapter extends RecyclerView.Adapter<BookAdapter.BookViewHolder> {

    private List<Book> bookList;
    private OnBookClickListener onBookClickListener;
    private int selectedPosition = -1; // 当前选中的位置

    public interface OnBookClickListener {
        void onBookClick(Book book, int position);
    }

    public BookAdapter(List<Book> bookList, OnBookClickListener listener) {
        this.bookList = bookList;
        this.onBookClickListener = listener;
    }

    @NonNull
    @Override
    public BookViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_book, parent, false);
        return new BookViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BookViewHolder holder, int position) {
        Book book = bookList.get(position);
        holder.bind(book, position);
    }

    @Override
    public int getItemCount() {
        return bookList.size();
    }

    /**
     * 获取当前选中的书籍
     */
    public Book getSelectedBook() {
        if (selectedPosition >= 0 && selectedPosition < bookList.size()) {
            return bookList.get(selectedPosition);
        }
        return null;
    }

    /**
     * 清除选择状态
     */
    public void clearSelection() {
        int previousSelected = selectedPosition;
        selectedPosition = -1;
        if (previousSelected != -1) {
            notifyItemChanged(previousSelected);
        }
    }

    class BookViewHolder extends RecyclerView.ViewHolder {
        private TextView tvBookNumber;
        private TextView tvBookName;
        private View selectionIndicator;

        public BookViewHolder(@NonNull View itemView) {
            super(itemView);
            tvBookNumber = itemView.findViewById(R.id.tv_book_number);
            tvBookName = itemView.findViewById(R.id.tv_book_name);
            selectionIndicator = itemView.findViewById(R.id.selection_indicator);

            itemView.setOnClickListener(v -> {
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION) {
                    // 更新选中状态
                    int previousSelected = selectedPosition;
                    selectedPosition = position;

                    // 刷新之前选中的项和当前选中的项
                    if (previousSelected != -1) {
                        notifyItemChanged(previousSelected);
                    }
                    notifyItemChanged(selectedPosition);

                    // 回调点击事件
                    if (onBookClickListener != null) {
                        onBookClickListener.onBookClick(bookList.get(position), position);
                    }
                }
            });
        }

        public void bind(Book book, int position) {
            tvBookNumber.setText(book.getBookNumber());
            tvBookName.setText(book.getBookName());

            // 设置选中状态
            if (position == selectedPosition) {
                selectionIndicator.setBackgroundColor(
                        ContextCompat.getColor(itemView.getContext(), android.R.color.holo_blue_bright));
                itemView.setBackgroundColor(
                        ContextCompat.getColor(itemView.getContext(), android.R.color.holo_blue_light));
            } else {
                selectionIndicator.setBackgroundColor(
                        ContextCompat.getColor(itemView.getContext(), android.R.color.transparent));
                itemView.setBackgroundColor(
                        ContextCompat.getColor(itemView.getContext(), android.R.color.white));
            }
        }
    }
}